package com.bocai.dahuatech.ui.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.OrderListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.ui.adapter.MyOrderAdapterHolder;
import com.bocai.dahuatech.ui.pressent.MyorderPressent;
import com.bocai.dahuatech.ui.view.LoadMoreRecyclerView;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.SharePrefencesUtil;
import com.google.gson.Gson;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


/**
 * 订单搜索
 */
public class SearchMyOrderActivity extends BaseActivity implements MyorderPressent{
    @Bind(R.id.swipe_search)
    SwipeRefreshLayout swipeSearch;
    @Bind(R.id.lv_search_order)
    LoadMoreRecyclerView lvSearchOrder;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.tv_title_right)
    TextView tvRight;
    OrderListBean orderListBean;
    MyOrderAdapterHolder adapter;
    @Override
    protected int contentView() {
        return R.layout.activity_search_myorder;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.meorder);
        lvSearchOrder.setLayoutManager(new LinearLayoutManager(this));
        initEvent();
    }
    private void getData() {
        showLoading();
        getTestApi().getOrderList(SharePrefencesUtil.getCustomerCode(this),etSearch.getText().toString(),etSearch.getText().toString(),"",1)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            swipeSearch.setRefreshing(false);
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error", "zhang");
        }

        @Override
        public void onNext(BaseBean baseBean) {
            swipeSearch.setRefreshing(false);
            hideLoading();
            if (baseBean.getReturnNo().equals("1"))
            {
                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);
                orderListBean=new Gson().fromJson(result,OrderListBean.class);
                setDatas();
                if(orderListBean.getContent().size()==0)
                {
                    showToast(R.string.noserchorder);
                }
            }
        }
    };

    private void setDatas() {
        adapter=new MyOrderAdapterHolder(SearchMyOrderActivity.this,orderListBean.getContent(),this,"");
        lvSearchOrder.setAdapter(adapter);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData();
            }
        });
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    getData();
                }
                return false;
            }
        });
        swipeSearch.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
    }

    @Override
    public void setIsShow(boolean isShow) {

    }
}
